//
//  BookViewController.m
//  HelpThePoor
//
//  Created by van7ish on 2016/12/1.
//  Copyright © 2016年 van7ish. All rights reserved.
//

#import "BookViewController.h"
#import "BookTableViewCell.h"
#import "BookDetailViewController.h"

@interface BookViewController () <UITableViewDelegate, UITableViewDataSource>

/** 导航条 */
@property (nonatomic, strong) UIImageView * navBar;
/** 热门书籍列表 */
@property (nonatomic, strong) UITableView * bookTableView;
/** 数据源 */
@property (nonatomic, strong) NSMutableArray * bookArr;
/** 加载页数 */
@property (nonatomic, assign) NSInteger pageIndex;

@end

@implementation BookViewController

- (NSInteger)pageIndex{
    if (!_pageIndex) {
        _pageIndex = 1;
    }
    return _pageIndex;
}

- (UIImageView *)navBar{
    if (!_navBar) {
        _navBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 64)];
        _navBar.image = ImageNamed(@"daohangtiao");
    }
    return _navBar;
}

- (NSMutableArray *)bookArr{
    if (!_bookArr) {
        _bookArr = [NSMutableArray array];
    }
    return _bookArr;
}

- (UITableView *)bookTableView{
    if (!_bookTableView) {
        _bookTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_W, SCREEN_H - 64) style:UITableViewStylePlain];
        _bookTableView.delegate = self;
        _bookTableView.dataSource = self;
        _bookTableView.backgroundColor = COLOR_LIGHTBGCOLOR;
        _bookTableView.rowHeight = HeightScale(160);
        _bookTableView.tableFooterView = [UIView new];
        _bookTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _bookTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestBook:)];
        _bookTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestBook:)];
    }
    return _bookTableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.navBar];
    
    [self.view addSubview:self.bookTableView];
    
    [self.bookTableView.mj_header beginRefreshing];
    
}

- (void)dealloc{
    DLog(@"%s", __func__);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.bookArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"bookCell";
    BookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[BookTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    cell.titleLabel.text = [NSString stringWithFormat:@"%@", self.bookArr[indexPath.row][@"name"]];
    cell.descLabel.text = [NSString stringWithFormat:@"%@", self.bookArr[indexPath.row][@"summaryText"]];
    cell.authorLabel.text = [NSString stringWithFormat:@"%@", [self.bookArr[indexPath.row][@"author"] length] ? self.bookArr[indexPath.row][@"author"] : @"连环画"];
    
    NSString *coverImageFile = [NSString stringWithFormat:@"%@%@", KHostAddress, self.bookArr[indexPath.row][@"coverImageFile"]];
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:coverImageFile] placeholderImage:ImageNamed(@"placeholdershuji")];

    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DLog(@"index = %ld", indexPath.row);
    
    BookDetailViewController *bookDetailVC = [[BookDetailViewController alloc] init];
    bookDetailVC.bookDic = self.bookArr[indexPath.row];
    [self.navigationController pushViewController: bookDetailVC animated:YES];
}

#pragma mark - data request

/** 热门书籍 */
- (void)requestBook:(UIView *)view{
    if ([view isKindOfClass:[MJRefreshNormalHeader class]]) {
        self.pageIndex = 1;
        [self.bookArr removeAllObjects];
    } else {
        self.pageIndex += 1;
    }
    
    NSString *URL = [NSString stringWithFormat:KBook, KHostAddress, KOrganizationID, @(self.pageIndex)];
    [ZRRequestManager POST:URL parameters:nil success:^(id responseObject) {
        DLog(@"%@", responseObject);
        if ([[responseObject objectForKey:@"error"] integerValue] == 0) {
            
            [self.bookArr addObjectsFromArray:responseObject[@"data"][@"items"]];
            
            [self.bookTableView reloadData];
        }
        [self.bookTableView.mj_header endRefreshing];
        [self.bookTableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        DLog(@"%@", error);
        [self.bookTableView.mj_header endRefreshing];
        [self.bookTableView.mj_footer endRefreshing];
    }];
}

@end
